#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_EQ_JAZZ		(0)
#define ITEM_EQ_POP		(1)
#define ITEM_EQ_CLASSIC		(2)
#define ITEM_EQ_FLAT		(3)
#define ITEM_EQ_ROCK		(4)
#define ITEM_EQ_OFF		(5)
#define ITEM_EQ_END		(6)
#define ITEM_EQ_UPDATE	(7)

TRect		AudioEQTouchRect[IMG_AUDIO_EQ_ITEM_QTY];
TPosCoor	AudioEQItemPos[IMG_AUDIO_EQ_ITEM_QTY] = { {50,94}, {50,187}, {50,279}, {221,93}, {221,187}, {221,279},{577,375} };
TPosCoor	AudioEQDispPos = {400,94};
char*		AudioEQInfoStr[IMG_AUDIO_EQ_ITEM_QTY] = {"JAZZ","POP","CLASC","FLAT","ROCK","OFF","END"};
int		AudioEQFocus = -1;
TFrame		AudioEQFrame;
//unsigned char EQ_data [5] = {0xe0,0x03,0x06,0x02,0xeb};
extern  unsigned char send_data_buf[5];
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
static unsigned char cur_EQ;
extern unsigned char rece_handle;
extern unsigned char dtpresent_flag;	
extern pthread_cond_t dt_cond;

int EQInfoUpdata (void)
{
	if (read_info_data[2] == ID_EQ_SEL)
		if(read_info_data[3])
			{
				cur_EQ = read_info_data[3];	
			}			
}


void AudioEQDrawBackgroup(TCanvas *pCanvas)
{	
	char FileName[20];
	int GetJpegIdx;	

	GetJpegIdx = IMG_AUDIO_EQ_OFF;

	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_EQ_FOLDER, GetJpegIdx);	
	//DrawJpeg(pCanvas, 0, 0, "/res_jpeg/audio_eq.jpg" );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	DrawJpeg(pCanvas, AudioEQDispPos.x, AudioEQDispPos.y, FileName);
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "EQ SET" );

}



void   AudioEQDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );				
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "EQ SET" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				  AudioEQDrawVolume(AudioEQFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void AudioEQDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;	
	int offset =0;
	
	if( idx < 0 || idx >= IMG_AUDIO_EQ_ITEM_QTY )
		return;
	if ((idx !=ITEM_EQ_END) && ( bfocus))
		{
		GetJpegIdx = IMG_AUDIO_EQ_JAZZ +idx;
		sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_EQ_FOLDER, GetJpegIdx);	
		DrawJpeg(pCanvas, AudioEQDispPos.x, AudioEQDispPos.y, FileName);
		}	
	else
		if (idx ==ITEM_EQ_END)
		offset = 1;
		
	if( bfocus )
		GetJpegIdx = IMG_AUDIO_EQ_ITEM_FOCUS +offset;
	else
		GetJpegIdx = IMG_AUDIO_EQ_ITEM +offset;
	
	pos = AudioEQItemPos[idx];	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_EQ_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);	
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	AudioEQTouchRect[idx].left = pos.x;
	AudioEQTouchRect[idx].right = pos.x + pJpeg.Width;
	AudioEQTouchRect[idx].top = pos.y;
	AudioEQTouchRect[idx].bottom = pos.y + pJpeg.Height;	
	g_Puts( pCanvas, FONT33, AudioEQTouchRect[idx], TEXT_ALIGN_CENTER, AudioEQInfoStr[idx]);
	
	
}

void AudioEQDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		AudioEQDrawBackgroup(AudioEQFrame.pCanvas);
	for( i = 0; i < IMG_AUDIO_EQ_ITEM_QTY; i++ )
	{
		if ((i == cur_EQ-1) && (i != 6))
			{				
				AudioEQDrawItem( AudioEQFrame.pCanvas, i, TRUE );
			}
		else
			AudioEQDrawItem( AudioEQFrame.pCanvas, i, FALSE );
	}
		if (mute_status == 1)
		{
			AudioEQDrawVolume(AudioEQFrame.pCanvas,1);
		}

	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int AudioEQGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_AUDIO_EQ_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, AudioEQTouchRect[i] ) )
			return i;
	return -1;
}

BOOL AudioEQBtnEnter(int idx)
{
	
	AudioEQDrawItem( AudioEQFrame.pCanvas, idx, FALSE );
	switch(idx)
	{
/*		case ITEM_EQ_JAZZ:
		case ITEM_EQ_POP:
		case ITEM_EQ_CLASSIC:
		case ITEM_EQ_FLAT:
		case ITEM_EQ_ROCK:
			dt_Present();
			break;
*/
		case ITEM_EQ_END:
			MenuAudioInit();
			break;
		case	ITEM_EQ_UPDATE:
			AudioEQInit();
			break;
	}
	//LOG( "btn = %d\n", idx );	

	return TRUE;
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
	switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
																									
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									AudioEQDrawVolume(AudioEQFrame.pCanvas,1);	
									break;
								}
									//ReadInfoKey = NO_INFO; //if handled, clear this flag.
									//rc = 0;//if handled, clear this flag.
								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:
										 AudioEQDrawVolume(AudioEQFrame.pCanvas,1);	
										break;
									case	ID_DMUTE:	
										 AudioEQDrawVolume(AudioEQFrame.pCanvas,3);	
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									case 	ID_FMAM:
										//band command;
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;			 
						break;
						
					case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;			
						break;	
					case	ID_EQ_SEL:
						AudioEQBtnEnter(ITEM_EQ_UPDATE);
						read_info_data[0] = 0;
						rc = 1;
						break;
					default:	
							rc = -1;						
						}
		return 0;						
}


void AudioEQInit()
{
	ir_SetCurrentID( IR_AUDIO_EQ_INIT );

	frm_Create( &AudioEQFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	AudioEQDraw( TRUE );
}
/*
static int send_data_out ( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_EQ_ITEM_QTY))
		return -1;
		
		if (index == ITEM_EQ_END)
			{
				EQ_data [2] = 0x07;
				EQ_data [3] = 0x01;
				EQ_data [4] = 0xeb;
			}
			else
			{
				EQ_data [2] = 0x06;
				EQ_data [3] = index+0x02;
				EQ_data [4] = (unsigned char )(0xeb + index);
			}
		if (dev_SPIWrite(EQ_data,5)==0)
			return 0;//hangsheng 071126
		return -1;
		
}
*/

static int send_data( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_EQ_ITEM_QTY))
		return -1;
		
		if (index == ITEM_EQ_END)
			{
				send_data_buf [2] = IDDATA_ESC;
				send_data_buf [3] = ESC_BUTTON;
			}
			else
			{
				send_data_buf [2] = IDDATA_AUDIO;
				send_data_buf [3] = index+AUDIO_JAZZ;
			}

			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
	return -1;
}

void AudioEQInput(UInt32 key)
{
			int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( AudioEQFocus = AudioEQGetTouchID( pos )) != -1 )
				{
					AudioEQDrawItem( AudioEQFrame.pCanvas, AudioEQFocus, TRUE );					
					dt_Present_part(AudioEQTouchRect [AudioEQFocus]);					
					if ( send_data(AudioEQFocus))
					printf ("write failure !\n");	
					
				}
			}
		}
		else
		{
			if( ( focus = AudioEQGetTouchID( pos ) ) == AudioEQFocus )
				AudioEQBtnEnter( AudioEQFocus );
			else if( AudioEQFocus != -1 )
			{
				if ( AudioEQFocus == ITEM_EQ_END)
					AudioEQDrawItem( AudioEQFrame.pCanvas, ITEM_EQ_END, FALSE );
					dt_Present_part(AudioEQTouchRect [ITEM_EQ_END]);
				//dt_Present();
			}
			AudioEQFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

